package com.group.flagtivities;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Friend extends Activity implements OnClickListener {
	
	private static LinearLayout user;
	private static LinearLayout requestResults;
	private static LinearLayout pendingResults;
	private static LinearLayout friendResults;
	private static ImageView search;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friend);
				
		findViews();
		configure();
		configureUser();
		
		TextUtil.setFont((ViewGroup) findViewById(R.id.friend), getAssets());
		
		populateResults();
	}
	
	private void findViews() {
		user = (LinearLayout) findViewById(R.id.user);
		requestResults = (LinearLayout) findViewById(R.id.requestResults);
		pendingResults = (LinearLayout) findViewById(R.id.pendingResults);
		friendResults = (LinearLayout) findViewById(R.id.friendResults);
		search = (ImageView) findViewById(R.id.search);
	}
	
	private void configure() {
		search.setOnClickListener(this);
	}
	
	private void configureUser() {
		TextView view = ViewUtil.generateDefaultTextView(this, Flagtivities.name);
		view.setTextColor(getResources().getColor(R.color.gray_dark));
		view.setShadowLayer(0.0f, 0.0f, 0.0f, getResources().getColor(R.color.white));
		view.setBackgroundColor(getResources().getColor(R.color.white));
		configureProfilePicture(view, Flagtivities.user_id);
		
		view.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(Friend.this, FriendFlags.class);
				i.putExtra("TITLE", "Your Flags");
				i.putExtra("friend_id", Flagtivities.user_id);
				Friend.this.startActivityForResult(i, 0);
			}
		});
		
		user.addView(view);
	}
	
	private void sort(ArrayList<FriendObject> friends) {
		Comparator<FriendObject> comparator = new Comparator<FriendObject>() {
				public int compare(FriendObject a, FriendObject b) {
					String nameA = a.first + " " + a.last;
					String nameB = b.first + " " + b.last;
					return nameA.compareTo(nameB);
				}
		};
		
		Collections.sort(friends, comparator);
	}
	
	private void populateResults() {
		sort(Flagtivities.FriendList);
		sort(Flagtivities.FriendRequestList);
		sort(Flagtivities.FriendPendingList);
		
		createResults(requestResults, Flagtivities.FriendRequestList);
		createResults(pendingResults, Flagtivities.FriendPendingList);
		createResults(friendResults, Flagtivities.FriendList);
		
		if (Flagtivities.FriendRequestList.size() == 0 && Flagtivities.FriendPendingList.size() == 0 && Flagtivities.FriendList.size() == 0) {
			TextView view = ViewUtil.generateDefaultTextView(this, "No Friends found.");
			view.setGravity(Gravity.CENTER);
			friendResults.addView(view);
		}
	}
	
	private void createResults(LinearLayout linearLayout, ArrayList<FriendObject> friends) {
		linearLayout.removeAllViews();
		
		if (friends.size() > 0) {
			for (FriendObject friend : friends) {
				if (linearLayout == requestResults) {
					createFriendRequest(friend);
				}else if (linearLayout == pendingResults) {
					createFriendPending(friend);
				}else if (linearLayout == friendResults) {
					createFriend(friend);
				}
			}
		}
	}

	private void configureProfilePicture(TextView view, int friend_id) {
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
        ImageUtil.download("http://flagtivities.netau.net/profile_pics/" + friend_id + ".jpg", view, metrics);
	}
	
	private void createFriendRequest(final FriendObject friend) {
		RelativeLayout r = ViewUtil.generateDefaultRelativeLayout(this, getResources().getDrawable(R.drawable.background_recent));
		TextView v = ViewUtil.generateDefaultTextView(this, friend.first + " " + friend.last);
		ImageView add = ViewUtil.generateDefaultImageView(this, getResources().getDrawable(R.drawable.friend_add));
		
		configureProfilePicture(v, friend.friend_id);
		
		add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				for (FriendObject i : Flagtivities.FriendRequestList) {
					if (i.friend_id == friend.friend_id) {
						Flagtivities.FriendRequestList.remove(i);
						Flagtivities.FriendList.add(i);
						WebUtil.addFriend(Flagtivities.user_id, friend.friend_id);
						break;
					}
				}
				populateResults();
			}
		});

		r.addView(v);
		r.addView(add);
		requestResults.addView(r);
	}
	
	private void createFriendPending(final FriendObject friend) {
		RelativeLayout r = ViewUtil.generateDefaultRelativeLayout(this, getResources().getDrawable(R.drawable.background_recent));
		TextView v = ViewUtil.generateDefaultTextView(this, friend.first + " " + friend.last);
		ImageView unfriend = ViewUtil.generateDefaultImageView(this, getResources().getDrawable(R.drawable.friend_pending));
		
		configureProfilePicture(v, friend.friend_id);
				
		unfriend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				for (FriendObject i : Flagtivities.FriendPendingList) {
					if (i.friend_id == friend.friend_id) {
						Flagtivities.FriendPendingList.remove(i);
						WebUtil.unFriend(Flagtivities.user_id, friend.friend_id);
						break;
					}
				}
				populateResults();
			}
		});
		
		r.addView(v);
		r.addView(unfriend);
		pendingResults.addView(r);
	}
	
	private void createFriend(final FriendObject friend) {
		RelativeLayout r = ViewUtil.generateDefaultRelativeLayout(this, getResources().getDrawable(R.drawable.background_recent));
		TextView v = ViewUtil.generateDefaultTextView(this, friend.first + " " + friend.last);
		ImageView remove = ViewUtil.generateDefaultImageView(this, getResources().getDrawable(R.drawable.friend_remove));
		
		configureProfilePicture(v, friend.friend_id);
		
		v.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(Friend.this, FriendFlags.class);
				i.putExtra("TITLE", friend.first + "'s Flags");
				i.putExtra("friend_id", friend.friend_id);
				Friend.this.startActivityForResult(i, 0);
			}
		});
	
		remove.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				for (FriendObject i : Flagtivities.FriendList) {
					if (i.friend_id == friend.friend_id) {
						Flagtivities.FriendList.remove(i);
						WebUtil.removeFriend(Flagtivities.user_id, friend.friend_id);
						break;
					}
				}
				populateResults();
			}
		});

		r.addView(v);
		r.addView(remove);
		friendResults.addView(r);
	}
	
	@Override
	protected void onResume() {
		populateResults();
		
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close:
			finish();
			break;
		case R.id.search:
			startActivity(new Intent(this, FriendSearch.class));
			break;
		}
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0) {
			if (resultCode == FriendFlags.GO_TO_MAP) finish();
		}
	}
	
}
